package com.zekdot.userconsumer.action;

import com.alibaba.fastjson.JSONObject;
import com.zekdot.userconsumer.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Create by zekdot on 20-4-19.
 */
@Controller
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String add(UserEntity userEntity){
        Map map = JSONObject.parseObject(JSONObject.toJSONString(userEntity),Map.class);
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        for(Object key :map.keySet()){
            parammap.add((String)key,map.get(key));
        }
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/add",entity,String.class);
//        return "???";
    }

    @RequestMapping(value = "/addList",method = RequestMethod.POST)
    @ResponseBody
    public String addList(String data){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("data",data);
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/addList",entity,String.class);
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public String register(UserEntity userEntity) {
        Map map = JSONObject.parseObject(JSONObject.toJSONString(userEntity),Map.class);
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        for(Object key :map.keySet()){
            parammap.add((String)key,map.get(key));
        }
        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/register",entity,String.class);
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(String username,String password) {
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("username",username);
        parammap.add("password",password);

        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/login",entity,String.class);
    }

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String get(String userId){
        return restTemplate.getForObject("http://user-client/user/get?userId="+userId,String.class);
    }

    @RequestMapping(value = "/getOther",method = RequestMethod.GET)
    @ResponseBody
    public String getOther(String username){
        return restTemplate.getForObject("http://user-client/user/getOther?username=" + username,String.class);
    }

    /**
     * 更新用户状态
     * @return
     */
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    @ResponseBody
    public String updateUserStatus(String username,int status){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("username",username);
        parammap.add("status",status);

        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/updateStatus",entity,String.class);
    }

    /**
     * 获取更多用户
     * @return
     */
    @RequestMapping(value = "/moreUsers",method = RequestMethod.GET)
    @ResponseBody
    public String getMoreUsers(String lastUsername,int length){
        return restTemplate.getForObject("http://user-client/user/moreUsers?lastUsername="
                +lastUsername + "&length=" + length,String.class);
    }

    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    public String changePassword(String userId,String oriPassword,String newPassword){
        HttpHeaders headers = new HttpHeaders();

        MultiValueMap<String, Object> parammap = new LinkedMultiValueMap<>();
        parammap.add("userId",userId);
        parammap.add("oriPassword",oriPassword);
        parammap.add("newPassword",newPassword);

        HttpEntity<Map> entity = new HttpEntity<>(parammap,headers);
        return restTemplate.postForObject("http://user-client/user/changePassword",entity,String.class);
    }
}
